package com.cn.zengzeng.spbzengzeng.config;

import com.cn.zengzeng.spbzengzeng.utils.LocalThreadPoolTaskExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.*;

/**
 * 初始化默认线程池
 * @Author: zhangzengzeng
 * @Date: 2021/7/16 11:45
 */
@Configuration
@Slf4j
public class ThreadPoolConfig {

    @Bean("asyncExecutor")
    public Executor asyncExecutor() {
        log.info("初始化线程池");
        LocalThreadPoolTaskExecutor executor = new LocalThreadPoolTaskExecutor();
        final int corePoolSize = Runtime.getRuntime().availableProcessors()+1 ;
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(corePoolSize*2);
        executor.setQueueCapacity(1000);
        executor.setKeepAliveSeconds(0);
        executor.setThreadNamePrefix("asyncExecutor");
        executor.initialize();
        return executor;
    }
}
